import { app } from "electron";
import {
  createMainWindow,
  createSpeedBallWindow,
  getMainWindow,
  getSpeedBallWindow,
  toggleSpeedBall,
  closeAllWindows,
} from "./modules/windowManager";
import { createDataMonitor } from "./modules/dataMonitor";
import { registerIpcHandlers } from "./ipcHandlers";

let dataMonitor: ReturnType<typeof createDataMonitor>;

function updateDataMonitorWindows() {
  dataMonitor.updateWindows(getMainWindow(), getSpeedBallWindow());
}

app.whenReady().then(() => {
  // 创建主窗口
  const mainWindow = createMainWindow();

  // 创建数据监控器
  dataMonitor = createDataMonitor(mainWindow);

  // 注册 IPC 处理器
  registerIpcHandlers({
    getMainWindow,
    getSpeedBallWindow,
    toggleSpeedBall,
    updateDataMonitorWindows,
  });

  // 主窗口准备就绪后开始数据监控
  mainWindow.once("ready-to-show", async () => {
    await dataMonitor.startMonitoring();
  });
});

app.on("window-all-closed", () => {
  if (process.platform !== "darwin") {
    app.quit();
  }
});

app.on("activate", () => {
  if (getMainWindow() === null) {
    createMainWindow();
  }
});
